package com.sds.sweng.swat.miplatform.ui.design.parts;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LayoutManager;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;

import com.sds.sweng.swat.miplatform.ui.design.models.MiplatformComponent;
import com.sds.sweng.swat.miplatform.ui.design.models.MiplatformContainer;
import com.sds.sweng.swat.miplatform.ui.design.models.MiplatformDesignModel;
import com.sds.sweng.swat.miplatform.ui.design.models.area.sub.HorizontalSubAreaContainer;
import com.sds.sweng.swat.miplatform.ui.design.models.area.sub.SubArea;
import com.sds.sweng.swat.miplatform.ui.design.parts.area.sub.HorizontalSubAreaContainerPart;

public abstract class MiplatformComponentPart extends AbstractGraphicalEditPart implements PropertyChangeListener {

	public LayoutManager getLayout() {
		return getFigure().getLayoutManager();
	}

	public LayoutManager getParentLayout() {
		return ((AbstractGraphicalEditPart) getParent()).getFigure().getLayoutManager();
	}

	public IFigure getParentFigure() {
		return ((AbstractGraphicalEditPart) getParent()).getFigure();
	}

	public boolean isSelectable() {
		return false;
	}

	protected void refreshVisuals() {

	}

	public void activate() {
		super.activate();
		getComponent().addPropertyChangeListener(this);
	}

	public void deactivate() {
		super.deactivate();
		getComponent().removePropertyChangeListener(this);
	}

	public void propertyChange(PropertyChangeEvent evt) {
		String prop = evt.getPropertyName();

		if (MiplatformComponent.CHILDREN.equals(prop)) {
			refreshChildren();
		} else {
			refreshVisuals();
		}

		((GraphicalEditPart) (getViewer().getContents())).getFigure().revalidate();
	}

	protected MiplatformComponent getComponent() {
		return (MiplatformComponent) getModel();
	}

	protected MiplatformDesignModel getRootModel() {
		return (MiplatformDesignModel) getViewer().getContents().getModel();
	}

	protected int getAreaWidth() {
		EditPart parentPart = getParent();
		EditPart childPart = this;

		while (parentPart != null) {
			if (parentPart instanceof MiplatformDesignPart) {
				return ((MiplatformDesignModel) parentPart.getModel()).getAreaWidth();
			} else if (parentPart instanceof HorizontalSubAreaContainerPart) {
				return ((HorizontalSubAreaContainer) parentPart.getModel()).getAreaWidth((SubArea) childPart.getModel());
			}

			childPart = parentPart;
			parentPart = parentPart.getParent();
		}

		return 0;
	}

	protected int getContentsWidth() {
		EditPart parentPart = getParent();
		EditPart childPart = this;

		while (parentPart != null) {
			if (parentPart instanceof MiplatformDesignPart)
				return ((MiplatformDesignModel) parentPart.getModel()).getContentsWidth();
			else if (parentPart instanceof HorizontalSubAreaContainerPart) {
				return ((HorizontalSubAreaContainer) parentPart.getModel()).getContentsWidth((SubArea) childPart.getModel());
			}

			childPart = parentPart;
			parentPart = parentPart.getParent();
		}

		return 0;
	}

	protected MiplatformContainer getContainerModel() {
		EditPart parentPart = getParent();

		while (parentPart != null) {
			if (parentPart.getModel() instanceof MiplatformDesignModel)
				return (MiplatformContainer) parentPart.getModel();

			parentPart = parentPart.getParent();
		}

		return null;
	}

}
